package com.jintoufs.service.image.impl;

import com.alibaba.fastjson.JSONObject;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.images.PlacardMapper;
import com.jintoufs.domain.images.Placard;
import com.jintoufs.service.image.PlacardService;
import com.jintoufs.util.CosUtil;
import com.qcloud.cos.request.UploadFileRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pengyang
 * Created by pengyang on 2017/12/27.
 */
@Service("placardService")
public class PlacadrdServiceImpl implements PlacardService {

    @Resource
    private PlacardMapper placardMapper;

    @Resource
    private CosUtil cosUtil;

    @Override
    public List<Placard> selectByparams(HashMap<String, Object> params) {
        return placardMapper.selectByParams(params);
    }

    @Override
    public String upToCos(CommonsMultipartFile[] files) {
        StringBuilder attachment = new StringBuilder();
        if (files != null) {
            try {
                for (CommonsMultipartFile file : files) {
                    synchronized (this) {
                        String originFileName = file.getOriginalFilename();
                        String fileExtension = originFileName.substring(originFileName.lastIndexOf("."));
                        if(".jpg.png.jpeg.gif".contains(fileExtension)) {
                            UploadFileRequest uploadFileRequest = new UploadFileRequest("default", "/messagePlacard/" + System.currentTimeMillis() + originFileName, file.getBytes());
                            try {
                                String result = cosUtil.uploadToCos(uploadFileRequest);
                                JSONObject object = JSONObject.parseObject(result);
                                if ("0".equals(object.get("code").toString())) {
                                    String path = JSONObject.parseObject(object.get("data").toString()).get("access_url").toString();
                                    path = path.replace("http://", "https://");
                                    attachment.append(path).append(",");
                                }else {
                                    attachment.append("error:").append(object.get("code").toString()).append(",");
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                                attachment.append("error").append(",");
                            }

                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return attachment.toString().substring(0,attachment.toString().length()-1);
    }

    @Override
    public Integer insert(Placard placards) {
        return placardMapper.insert(placards);
    }

    @Override
    public Integer delete(Integer id) {
        return placardMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(Placard placards) {
        return placardMapper.updateByPrimaryKeySelective(placards);
    }

    @Override
    public List<Map<String, Object>> getByFilter(QueryFilters filters_) {
        return placardMapper.getByFilter(filters_);
    }
}
